﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 冒泡排序
{
	class Program
	{
		static int calCount = 0;
		/// <summary>
		/// 冒泡排序
		/// </summary>
		/// <param name="data"></param>
		public static void BubbleSort(ref int[] data)
		{
			for (int i = 0; i < data.Length - 1; i++)
			{
				for (int j = data.Length - 1; j > i; j--)
				{
					calCount++;
					if (data[j] > data[j - 1])
					{
						data[j] = data[j] + data[j - 1];
						data[j - 1] = data[j] - data[j - 1];
						data[j] = data[j] - data[j - 1];
					}
				}
			}
		}

		static void Main(string[] args)
		{
			int[] intArr = new int[] { 10, 8, 3, 5, 6, 7, 9 };
			Console.Write("排序前：");
			for (int i = 0; i < intArr.Length; i++)
			{
				Console.Write(intArr[i] + " ");
			}
			Console.WriteLine();

		    BubbleSort(ref intArr);

			Console.Write("排序后：");
			for (int i = 0; i < intArr.Length; i++)
			{
				Console.Write(intArr[i] + " ");
			}
			Console.WriteLine();

			Console.WriteLine("计算次数：" + calCount);

			Console.ReadLine();
		}
	}
}
